<?php 
class GoodsGalleryModel extends Model{
	public $table='goods_gallery';

	public function __init(){

	}

	//获取商品相册
	public function getImage($gid){
		$map['goods_gid']=$gid;
		//同一商品可能有多张图片，all()取所有的
		$img=$this->where($map)->all();
		// p($img);exit;
		return $img;
	}

	//添加商品相册
	public function addImage($gid){
		// 上传类处理相册图片
		$upload=new Upload('Upload/goods/'.date('y/m/d'));
		$img=$upload->upload('gallery_img');
		//没有图片上传时不处理，直接返回
		if(empty($img)){
			return ;
		}
		//图片处理类
		$image=new Image();
		foreach ($img as $file) {
			$img_url=$file['dir'].$file['filename'].'_list_440X660.'.$file['ext'];
			$thumb_url=$file['dir'].$file['filename'].'_thumb_50X50.'.$file['ext'];
			$image->thumb($file['path'],$img_url,440,660,6);
			$image->thumb($file['path'],$thumb_url,57,57,6);
			$data=array(
				'goods_gid'=>$gid, //商品ID
				'img_original'=>$file['path'],  //原图
				'img_url'=>$img_url, //中图
				'thumb_url'=>$thumb_url,   //小图
			);
			M('goods_gallery')->add($data);
		}
	}

	//删除相册图片
	public function delImage($goods_gallery_id){
		$file=$this->find($goods_gallery_id);
		is_file($file['img_original']) && unlink($file['img_original']);
		is_file($file['img_url']) && unlink($file['img_url']);
		is_file($file['thumb_url']) && unlink($file['thumb_url']);
		return $this->del($goods_gallery_id);
	}
}

?>